const helper = new Helper(this);
this.widgetObj.sullivan.initial();

const width = 1568 * (document.body.offsetWidth / 1920);
const height = 980 * (document.body.offsetHeight / 1080);

console.log('组件全部加载完成！')

const $sullivanModal = this.widgetObj.sullivan.modal.container?.find('.base-sullivan-modal')
$sullivanModal?.css({width , height, 'z-index': '9'});
this.widgetObj.sullivan.setGrid([3, 3]);

// const $modalContainer = this.widgetObj.sullivan.modal.container;
// const videoAccessObj = this.pageObj.getApiWidgetByAliasName('video_access_container').widgetObj;
// console.log(videoAccessObj, 'videoAccessObj')
// $modalContainer.css({ width: '100%', height: '100%' })
// $sullivanModal?.css({ width: '100%', height: '100%', position: 'unset' });
// $(videoAccessObj.container).append($modalContainer);

// // esc事件
// $(document).on('keyup', function (e) {
//     if (e.keyCode === 27) {
//         console.log('键盘事件')
//         window.parent.postMessage({ type: 'nav/css', cssObj: { 'visibility': 'visible' } }, '*');
//     }
// });

const { proxy, handleSullivanData, baseRequest, selectView, handleMonitorData } = window._videoAccess;
const $footerLeft = $('.base-sullivan-modal-container .modal-footer-left');
const $saveBtn = $(`
<div
 class="base-sullivan-btn"
 style="width: var(--item-size, 28px); height: var(--item-size, 28px);"
 data-voice-module="监控视图"
 data-voice-text="保存"
 title="保存视图"
>
    <img src="/resource/datacolour/common/保存.png" style="width: 100%; height: 100%;">
</div>
`)
const $saveAsBtn = $(`
<div
 class="base-sullivan-btn"
 style="width: var(--item-size, 28px); height: var(--item-size, 28px);"
 data-voice-module="监控视图"
 data-voice-text="另存为"
 title="视图另存为"
>
    <img src="/resource/datacolour/common/另存为.png" style="width: 100%; height: 100%;">
</div>
`)

async function editView(monitorSelect) {
    const videoData = handleSullivanData();
    const url = `/${proxy}SummaryAssessmentSV/videoView/update`;
    // 保存视图
    await baseRequest(url, { id: monitorSelect.id, ...videoData }, 'POST');
    // 重新获取视图数据
    handleMonitorData();
    window.layer?.msg('保存成功');
}

if (!$footerLeft.children().length) {
    // 将按钮添加到视频窗口底部
    $footerLeft.append($saveBtn).append($saveAsBtn);

    // 点击【保存】按钮
    $saveBtn.on('click', () => {
        selectView(editView)
    });
    // 点击【另存为】按钮
    $saveAsBtn.on('click', () => {
        selectView((monitorSelect) => {
            // 为表单赋值 - 【保存视图】
            helper.get('view_save_form').widgetObj.setFormData('view_save_form', {
                viewName: '视图' + new Date().getTime(),
                viewType: '0',
                projectCode: 'cqdz',
            });
            // 显示保存弹窗
            helper.getByCls('save-modal', true)
        })
    });
}